五、Oracle 分组查询、视图

您所在的位置:网站首页 oracle 分组查询,分组统计 五、Oracle 分组查询、视图

五、Oracle 分组查询、视图

2024-07-13 08:46| 来源: 网络整理| 查看: 265

一。分组函数1.avg:平均分2.sum:求和3.max:最大值4.min:最小值注意:前面四个必须针对数值字段,且参数只能是一个5.count:求个数

二。分组查询1。语法是 group by 分组字段1,分组字段2...2.分组后可以使用分组函数。3.分组函数不能和其他字段一起显示,除了分组字段。4.分组查询之后还要做条件限制的话,用having子句5.关键字的顺序:select ...from...where...group by...having...order by....

三。子查询:在查询的结果上继续查询

1.将查询结果作为新的表继续查询select * from student a,(select * from grade) b where a.xxid=b.xxid2.将查询结果作为查询条件来使用select * from grade where student in (select st_id from student where st_sex='女')

四。视图(view)1.作用。保存查询结果,方便反复调用2.语法:create view 视图名 as 查询语句3.注意:视图的数据可以修改,但不建议修改。统计后的数据,用了函数的数据,以及非键保留值得数据都不能修改

 

 练习:

 

1.查询每门课程的最高分和最低分,显示如下 java最高分:99 最低分:33 oracle最高分:100 最低分: 55 select a.dic_name,max(b.socre) 最高分,min(b.socre) 最低分 ,avg(b.socre) 平均分,sum(b.socre) 总和 from diclipline a,grade b where a.dic_id=b.diclipline group by a.dic_name 2.查询每门成绩的平均分和总分 3.查询java1班参加oracle考试的人数 select count(rownum) from squad a3,diclipline b3,grade c3,student d3 where a3.squad_id=d3.st_class and b3.dic_id=c3.diclipline and c3.student=d3.st_id and a3.squad_name='java1班' and b3.dic_name='oracle' 4.查询每个班j2ee考试的平均分,并按降序排序 select a4.squad_name,avg(c4.socre) from squad a4,diclipline b4,grade c4,student d4 where a4.squad_id=d4.st_class and b4.dic_id=c4.diclipline and c4.student=d4.st_id and b4.dic_name='j2ee' group by a4.squad_name order by avg(c4.socre) desc 5.查询oracle考试平均成绩不及格的班级有哪些 select avg(c5.socre),a5.squad_name,b5.dic_name from squad a5,diclipline b5,grade c5,student d5 where a5.squad_id=d5.st_class and b5.dic_id=c5.diclipline and c5.student=d5.st_id and b5.dic_name='oracle' group by a5.squad_name,b5.dic_name having avg(c5.socre)>60 6.查询所有java考试不及格的学员是哪些 select * from grade where socre


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3